package com.shuoyue.ycgk.ui.mine.info;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.entity.DealLessonInfo;
import com.shuoyue.ycgk.entity.MouthInfo;
import com.shuoyue.ycgk.entity.UserInfo;
import com.shuoyue.ycgk.ui.course.SpecialCourseActivity;
import com.shuoyue.ycgk.ui.main.MainTabActivity;
import com.shuoyue.ycgk.ui.mine.collection.CollectionListActivity;
import com.shuoyue.ycgk.ui.mine.coupon.CouponVilidateActivity;
import com.shuoyue.ycgk.ui.mine.learnprogress.DealClassProgressActivity;
import com.shuoyue.ycgk.ui.mine.learnprogress.LearnProgressActivity;
import com.shuoyue.ycgk.ui.mine.learnprogress.contract.DealClassProgressContract;
import com.shuoyue.ycgk.ui.mine.mok.MyMokActivity;
import com.shuoyue.ycgk.ui.mine.mynote.NotePagerActivity;
import com.shuoyue.ycgk.ui.mine.mypaperset.MyPaperGroupActivity;
import com.shuoyue.ycgk.ui.mine.order.OrdersActivity;
import com.shuoyue.ycgk.ui.mine.vip.MyBuyVipActivity;
import com.shuoyue.ycgk.ui.mine.vip.MyVipActivity;
import com.shuoyue.ycgk.ui.mine.wrong.WrongListActivity;
import com.shuoyue.ycgk.ui.papergroups.running.myrush.UserRushActivity;
import com.shuoyue.ycgk.ui.shop.address.SelectAddressActivity;
import com.shuoyue.ycgk.utils.GlideUtil;
import com.shuoyue.ycgk.utils.XDateUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

public class FragmentMe extends BaseMvpFragment implements
        DealClassProgressContract.View,
        MemberInfoContract.View {
    @BindView(R.id.tv_message_count)
    TextView tvMessageCount;
    @BindView(R.id.head)
    ImageView head;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.time_count)
    TextView timeCount;
    @BindView(R.id.q_done)
    TextView qDone;
    @BindView(R.id.q_total)
    TextView qTotal;
    @BindView(R.id.right_rate)
    TextView rightRate;
    @BindView(R.id.lay_jion)
    LinearLayout layJion;
    @BindView(R.id.myClasse)
    Button myClasse;
    @BindView(R.id.join_num)
    TextView joinNum;
    MemberInfoContract.Presenter memberInfoPresenter;
    DealClassProgressContract.Presenter dealPresenter;
    UserInfo userInfo;


    @Override
    public void initParams(Bundle bundle) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {

    }


    @Override
    protected void initData() {
        dealPresenter = new DealClassProgressContract.Presenter();
        dealPresenter.attachView(this);
        memberInfoPresenter = new MemberInfoContract.Presenter();
        memberInfoPresenter.attachView(this);
        userInfo = SPUtils.getUserInfo(mContext);
        if (userInfo != null)
            setUserInfo();
        getDealProgress();
    }

    void setUserInfo() {
        GlideUtil.loadImageCircular(mContext,
                userInfo.getAvatar(),
                R.mipmap.index_head, head);
        tvNickname.setText(TextUtils.isEmpty(userInfo.getRealname()) ? userInfo.getUsername() : userInfo.getRealname());
        layJion.setVisibility(userInfo.getIsAgreement() == 0 ? View.VISIBLE : View.GONE);
        myClasse.setVisibility(userInfo.getIsAgreement() == 1 ? View.VISIBLE : View.GONE);
    }

    private void getDealProgress() {
        dealPresenter.getProgressInfo(XDateUtils.format(System.currentTimeMillis(), "yyyy-MM-dd"));
    }


    public static FragmentMe getInstance() {
        FragmentMe fragment = new FragmentMe();
        return fragment;
    }

    @OnClick({R.id.lay_message,
            R.id.head,
            R.id.myClasse,
            R.id.checkProgressInfo,
            R.id.wrong,
            R.id.collection,
            R.id.noted,
            R.id.test_run,
            R.id.lay_my_vip,
            R.id.myOrder,
            R.id.layMyCourse,
            R.id.layMok,
            R.id.my_set,
            R.id.layInvite,
            R.id.vipcenter,
            R.id.layAddress,
            R.id.setPlan,
            R.id.layCoupon})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lay_message:
                break;
            case R.id.head:
                startActivity(new Intent(mContext, UserInfoActivity.class));
                break;
            case R.id.myClasse:
                startActivity(new Intent(mContext, DealClassProgressActivity.class));
                break;
            case R.id.checkProgressInfo:
                startActivity(new Intent(mContext, LearnProgressActivity.class));
                break;
            case R.id.wrong:
                startActivity(new Intent(mContext, WrongListActivity.class));
                break;
            case R.id.collection:
                startActivity(new Intent(mContext, CollectionListActivity.class));
                break;
            case R.id.noted:
                startActivity(new Intent(mContext, NotePagerActivity.class));
                break;
            case R.id.test_run:
                startActivity(new Intent(mContext, UserRushActivity.class));
                break;
            case R.id.vipcenter:
            case R.id.lay_my_vip:
//                MyVipActivity.start(mActivity,false);
                startActivity(new Intent(mContext, MyBuyVipActivity.class));
                break;
            case R.id.myOrder:
                startActivity(new Intent(mContext, OrdersActivity.class));

                break;
            case R.id.layMyCourse:
                ((MainTabActivity) Objects.requireNonNull(getActivity())).goIndexpage(1);
                break;
            case R.id.layMok:
                startActivity(new Intent(mContext, MyMokActivity.class));
                break;
            case R.id.layInvite:
                startActivity(new Intent(mContext, ShareGiftActivity2.class));

                break;
            case R.id.layAddress:
                startActivity(new Intent(mContext, SelectAddressActivity.class));
                break;
            case R.id.setPlan:
                startActivity(new Intent(mContext, SpecialCourseActivity.class));
                break;
            case R.id.my_set:
                MyPaperGroupActivity.start(mContext, 0);

                break;
            case R.id.layCoupon:
                startActivity(new Intent(mContext, CouponVilidateActivity.class));
                break;
        }
    }

    @Override
    public void setLessonProcess(DealLessonInfo lessonProcess) {
        if (lessonProcess == null) {
            return;
        }
        timeCount.setText(lessonProcess.getTargetOfDayDTO().getMinuteNum() + "分钟");
        qDone.setText(String.valueOf(lessonProcess.getTargetOfDayDTO().getQuestionNum()));
        qTotal.setText(String.valueOf(lessonProcess.getTargetOfDayDTO().getSetQuestionNum()));
        rightRate.setText(String.valueOf(lessonProcess.getTargetOfDayDTO().getAccuracy() + "%"));
        rightRate.setTextColor(lessonProcess.getSumQuestions() > lessonProcess.getTargetOfDayDTO().getSetQuestionNum()
                ? Color.parseColor("#1C90F5")
                : Color.parseColor("#FE5118"));
        joinNum.setText(String.valueOf(lessonProcess.getTargetOfDayDTO().getTotalPerson()));
    }

    @Override
    public void setMouthInfo(Date date, List<MouthInfo> infos) {

    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if ((isVisibleToUser && isResumed())) {
            onResume();
        } else if (!isVisibleToUser) {
            onPause();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
        if (getUserVisibleHint()) {
            //TODO give the signal that the fragment is visible
            memberInfoPresenter.getMemberInfo();
            getDealProgress();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        //TODO give the signal that the fragment is invisible
    }

    @Override
    public void setMemberInfo(UserInfo userInfo) {
        if (this.userInfo != null) {
            userInfo.setToken(this.userInfo.getToken());
        }
        SPUtils.updateUserInfo(mContext, userInfo);
        GlideUtil.loadImageCircular(mContext,
                userInfo.getAvatar(),
                R.mipmap.index_head, head);
        tvNickname.setText(TextUtils.isEmpty(userInfo.getRealname()) ? userInfo.getUsername() : userInfo.getRealname());
        layJion.setVisibility(userInfo.getIsAgreement() == 0 ? View.VISIBLE : View.GONE);
        myClasse.setVisibility(userInfo.getIsAgreement() == 1 ? View.VISIBLE : View.GONE);
    }
}
